package com.hxy.travel.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.hxy.travel.constant.MessageConstant;
import com.hxy.travel.dao.MemberDao;
import com.hxy.travel.dao.OrderDao;
import com.hxy.travel.dao.OrderSettingDao;
import com.hxy.travel.entity.Result;
import com.hxy.travel.pojo.Member;
import com.hxy.travel.pojo.Order;
import com.hxy.travel.pojo.OrderSetting;
import com.hxy.travel.service.OrderService;
import com.hxy.travel.service.OrderSettingService;
import com.hxy.travel.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public Result addOrder(Map orderInfo) throws Exception {

        String orderDate = (String) orderInfo.get("orderDate");

        Date date = DateUtils.parseString2Date(orderDate);

        OrderSetting orderSetting = orderSettingDao.findOrderSettingOrderDate(date);

        if(orderSetting == null){
            return Result.fail(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }else{
            int number = orderSetting.getNumber();

            int reservations = orderSetting.getReservations();

            if ( reservations >= number ){
                return Result.fail(MessageConstant.ORDER_FULL);
            }

        }


        String telephone = (String) orderInfo.get("telephone");

        Member member = memberDao.findMemberByTelephone(telephone);

        if ( member != null ){

            Integer memberId = member.getId();

            String setmealId = (String) orderInfo.get("setmealId");

            Order order = new Order();

            order.setMemberId(memberId);

            order.setSetmealId(Integer.parseInt(setmealId));

            order.setOrderDate(date);

            List<Order> orderList = orderDao.findByOrder(order);

            if (orderList != null && orderList.size() > 0) {

                return Result.fail(MessageConstant.HAS_ORDERED);
            }
            }else {
                member = new Member();

                member.setName((String) orderInfo.get("name"));

                member.setSex((String) orderInfo.get("sex"));

                member.setPhoneNumber(telephone);

                member.setIdCard((String) orderInfo.get("idCard"));

                member.setRegTime(new Date());

                member.setName((String) orderInfo.get("name"));

                memberDao.addMember(member);

            }
            orderSetting.setReservations(orderSetting.getReservations()+1);

            orderSettingDao.editOrderSetting(orderSetting);

            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(date);
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            String setmealId = (String) orderInfo.get("setmealId");
            order.setSetmealId(Integer.parseInt(setmealId));
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            orderDao.addOrder(order);
            return Result.data(order,MessageConstant.ORDER_SUCCESS);
        }

    @Override
    public Map findByIdOrderInfo(Integer id) {
        Map map = orderDao.findByIdOrderInfo(id);
        Date orderDate = (Date) map.get("orderDate");
        try {
            String date2String = DateUtils.parseDate2String(orderDate);

            map.put("orderDate",date2String);

        } catch (Exception e) {

            e.printStackTrace();

        }
        return map;
    }



}
